package com.wz.project.pda.service;

import com.wz.framework.web.domain.AjaxResult;
import com.wz.project.api.barcode.domain.Barcode;
import com.wz.project.api.barcode.domain.BarcodeInfo;
import com.wz.project.api.barcode.domain.MatnrInfo;
import com.wz.project.api.barcode.mapper.*;
import com.wz.project.api.tmsMatnr.domain.TmMatnr;
import com.wz.project.api.tmsMatnr.mapper.TmMatnrMapper;
import com.wz.project.api.tmsMatnr.service.ITmMatnrService;
import com.wz.project.inStock.stockin.domain.InStockin;
import com.wz.project.inStock.stockin.mapper.InStockinMapper;
import com.wz.project.inStock.stockindetail.domain.InStockinDetail;
import com.wz.project.inStock.stockindetail.mapper.InStockinDetailMapper;
import com.wz.project.mydo.domain.MydoJob;
import com.wz.project.mydo.domain.MydoJobDetail;
import com.wz.project.mydo.mydojob.mapper.MydoJobMapper;
import com.wz.project.mydo.mydojobdetail.mapper.JobDetailMapper;
import com.wz.project.outStock.stockoutdetail.mapper.OutStockoutDetailMapper;
import com.wz.project.stock.stock.mapper.BasStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PdaServiceImpl implements PdaService {

    @Autowired
    BarcodeMapper barcodeMapper;
    @Autowired
    MatnrMapper matnrMapper;

    @Autowired
    MatnrBDMapper matnrBdMapper;
    @Autowired
    InStockinDetailMapper inStockinDetailMapper;
    @Autowired
    OutStockoutDetailMapper outStockoutDetailMapper;
    @Autowired
    BarcodeInfoMapper barcodeInfoMapper;
    @Autowired
    MydoJobMapper mydoJobMapper;
    @Autowired
    BasStockMapper basStockMapper;
    @Autowired
    InStockinMapper stockinMapper;
    @Autowired
    JobDetailMapper mydoJobDetailMapper;
    @Autowired
    private EntityManager entityManager;
    @Autowired
    private TmMatnrMapper tmMatnrMapper;

    @Autowired
    BarcodeBdMapper barcodeBdMapper;
    @Override
    public Barcode selectBarcode(String fbarcode) {
        return barcodeMapper.selectBarcodeById(fbarcode);
    }
    @Override
    public  Barcode selectBarcodeByIdHs(String fbarcode) {
        Map<String, String> map = new HashMap();
        map.put("barcode", fbarcode);
        map.put("FBARCODE", null);
        map.put("MATNR", null);
        map.put("FNUM", null);
        map.put("FUNIT", null);
        map.put("FFLAG", null);
        map.put("CREATETIME", null);
        map.put("AUFNR", null);
        map.put("PURCHASENO", null);
        map.put("UCODE", null);
        map.put("EBELP", null);

        barcodeMapper.selectBarcodeByIdHs(map);
        Barcode barcode = new Barcode();
        if ("".equals(map.get("FBARCODE")) && map.get("FBARCODE") != null) {
            barcode.setFbarcode(map.get("FBARCODE"));
            barcode.setMatnr(map.get("MATNR"));
            barcode.setFnum(map.get("FNUM"));
            barcode.setFunit(map.get("FUNIT"));
            barcode.setFflag(map.get("FFLAG"));
            barcode.setCreatetime(map.get("CREATETIME"));
            barcode.setAufnr(map.get("AUFNR"));
            barcode.setPurchaseno(map.get("PURCHASENO"));
            barcode.setUcode(map.get("UCODE"));
            barcode.setEbelp(map.get("EBLELP"));

        }
        return barcode;
    }
    @Override
    public List<MatnrInfo> selectMatnrInfo(String matnr) {
        return matnrMapper.selectMatnrsById(matnr);
    }

    @Override
    public  List<InStockinDetail> selectInstockDetail(String stockinGuid, String productId) {
        return inStockinDetailMapper.selectInstockDetail(stockinGuid, productId);
    }

    @Override
    public MatnrInfo selectMatnrById(String matnr) {
        return matnrMapper.selectMatnrById(matnr);
    }
    public MatnrInfo selectMatnrBdById(String matnr){
        return matnrBdMapper.selectMatnrById(matnr);
    }
    @Override
    public BarcodeInfo selectBarcodeInfo(String fbarcode) {
        return barcodeInfoMapper.selectBarcodeInfoById(fbarcode);
    }

    @Override
    public int selectJobByTray(String trayCode, String status) {
        return mydoJobMapper.selectJobBytrayCode(trayCode, status);
    }

    @Override
    public AjaxResult insetMydoJob(String trayCode, Integer quantity, String fbarcode) {

        String msg = "";
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_auto_in_product");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_barcode_detail", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_in_quantity", Integer.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_tray_code", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);

        parameter.setParameter("v_barcode_detail", fbarcode);
        parameter.setParameter("v_in_quantity", quantity);
        parameter.setParameter("v_tray_code", trayCode);


        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");

        if ("1".equals(result)) {
            msg = "该托盘已有作业！";
        } else if ("2".equals(result)) {
            msg = "子件标签不存在！";
        }else if ("3".equals(result)) {
            msg = "子件标签已扫描！";
        }else if ("4".equals(result)) {
            msg = "该托盘已有出库作业！";
        }else if ("5".equals(result)) {
            msg = "该子件标签未过账！";
        } else if ("9".equals(result)) {
            msg = "操作失败，请重试！";
        } else if ("10".equals(result)) {
            msg = "操作成功！";
        }

        if ("10".equals(result)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    @Override
    public int updateJobStatus(String trayCode) {

        return mydoJobMapper.updateByTray(trayCode);
    }

    @Override
    public TmMatnr selectTmMatnrByFid(String fbarcode) {
        return tmMatnrMapper.selectTmMatnrById(fbarcode);
    }

    @Override
    public BarcodeInfo selectBarcodeInfoByPro(String matnr, String barcodeMain,String barcodeBehind) {
        return barcodeInfoMapper.selectBarcodeInfoByPro(matnr, barcodeMain,barcodeBehind);
    }

    @Override
    public AjaxResult insertMydoJobFinish(String cargoSpaceId, String fbarcode, Integer quantity,String trayCode) {

        String msg = "";
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_auoto_in_productP");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_barcode_detail", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_in_quantity", Integer.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_tray_code", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_cargo_space_id", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);

        parameter.setParameter("v_barcode_detail", fbarcode);
        parameter.setParameter("v_in_quantity", quantity);
        parameter.setParameter("v_tray_code", trayCode);
        parameter.setParameter("v_cargo_space_id", cargoSpaceId);


        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");

        if ("1".equals(result)) {
            msg = "该托盘已有作业！";
        } else if ("2".equals(result)) {
            msg = "子件标签不存在！";
        } else if ("3".equals(result)) {
            msg = "平库不存在此货位！";
        }  else if ("9".equals(result)) {
            msg = "操作失败，请重试！";
        } else if ("10".equals(result)) {
            msg = "操作成功！";
        }else if ("4".equals(result)) {
            msg = "扫描重复！";
        }

        if ("10".equals(result)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }



    }

    @Override
    public String completeJob(String trayCode) {

        MydoJob mydoJob = mydoJobMapper.selectJob(trayCode);
        if (mydoJob != null) {

        StoredProcedureQuery storedProcedureQuery =
                entityManager.createStoredProcedureQuery("IMP_COMPLETE");
        //赋值
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_task_no", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);

        parameter.setParameter("v_task_no", mydoJob.getTaskNo());
        //执行
        storedProcedureQuery.execute();

        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");
        if("1".equals(result)|| "2".equals(result)){
            return "1";
        }
        else{
            return "2";
        }


        }else{
            return "1";
        }


    }


    @Override
    public List<Barcode> selectBarcodeInfoByMatnr(String Matnr) {
        return barcodeMapper.selectListByMatnr(Matnr);
    }

    @Override
    public List<Barcode> selectBarcodeBycode(String barcodeMain) {
        return barcodeMapper.selectBarcodeBycode(barcodeMain);
    }
    @Override
    public Barcode selectBarcodeBd(String fbarcode) {
        return barcodeBdMapper.selectBarcodeById(fbarcode);
    }

    @Override
    public int saveBarcode(Barcode barcode) {
        return barcodeBdMapper.insertInto(barcode);
    }
    @Override
    public List<TmMatnr> selectTmMatnrByfbarcode(String barcodeMain, String barcodeBehind) {
        return tmMatnrMapper.selectTmMatnrByfbarcode(barcodeMain,barcodeBehind);
    }
    @Override
    public List<TmMatnr> selectTmMatnrByfbarcodea(String fbarcode) {
        return tmMatnrMapper.selectTmMatnrByfbarcodea(fbarcode);
    }
    @Override
    public int selectCount(String stockInId) {
        return inStockinDetailMapper.findsum(stockInId);
    }
    @Override
    public int selectCountAll(String stockInId) {
        return inStockinDetailMapper.findsumall(stockInId);
    }
    @Override
    public int selectCountOut(String stockoutId) {
        return outStockoutDetailMapper.findsumout(stockoutId);
    }

    @Override
    public List<MatnrInfo> selectMatnrInfobd(String matnr) {
        return matnrBdMapper.selectMatnrsById(matnr);
    }

    @Override
    public void insetMatnrInfo(MatnrInfo m) {
        matnrBdMapper.insertMatnrInfo(m);
    }

    @Override
    public void deletetmMatnrs(String stockInId) {
        tmMatnrMapper.deleteTmMatnrByStickInId(stockInId);
    }

    @Override
    public void finishPK() {
        HashMap<String, Object> map = new HashMap<>();
        tmMatnrMapper.finishPK(map);
    }
    @Override
    public void wms_data_in_del() {
        HashMap<String, Object> map = new HashMap<>();
        tmMatnrMapper.wms_data_in_del(map);
    }
    @Override
    public int selectTmMatnrByFCount(String productDetailId, String stockoutGuid,String productId) {

        int aa = tmMatnrMapper.selectTmMatnrByFCount(productDetailId,stockoutGuid);

        int i = tmMatnrMapper.selectOutStockByFCount(stockoutGuid, productId);
        if(i>aa){
            return 0;
        }else{
            return 1;
        }

    }

    @Override
    public InStockinDetail selectInstock(String stockinGuid) {
        return inStockinDetailMapper.selectCount(stockinGuid);
    }

    @Override
    public List<TmMatnr> selectTmMatnrByStockGuid(String stockInId) {
        return tmMatnrMapper.seleteTmMatnrByStickInId(stockInId);
    }

    @Override
    public  List<TmMatnr>  selectTmMatnrList(String stockinGuid, String matnr) {
        TmMatnr tmMatnr = new TmMatnr();
        tmMatnr.setMatnr(matnr);
        tmMatnr.setInStockId(stockinGuid);
        return tmMatnrMapper.selectTmMatnrList(tmMatnr);
    }

    @Override
    public void updatemMatnrs(String stockInId) {
        tmMatnrMapper.updateTmMatnr1(stockInId);
    }

    @Override
    public List<MydoJobDetail> selectJobDet(String trayCode) {
        return mydoJobDetailMapper.selectJobDetailByTrayCode(trayCode);
    }

    @Override
    public void updateInstockDetail(String stockinGuid, String matnr,String stockinDetailId) {
        inStockinDetailMapper.updateInstockDetail(stockinGuid,matnr,stockinDetailId);
    }

    @Override
    public AjaxResult addStock(String trayCode, String cargoSpaceId, String barcode) {
        String msg = "";
        AjaxResult ajaxResult = new AjaxResult();
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_add_stock");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_cargoSpaceId", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_tray_code", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_barcode", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);

        parameter.setParameter("v_cargoSpaceId", cargoSpaceId);
        parameter.setParameter("v_tray_code", trayCode);
        parameter.setParameter("v_barcode", barcode);

        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");

        if ("1".equals(result)) {
            msg = "扫描重复！";
        } else if ("2".equals(result)) {
            msg = "货位不存在！";
        } else if ("3".equals(result)) {
            msg = "托盘不在该储位！";
        }  else if ("4".equals(result)) {
            msg = "扫描信息不存在！";
        }    else if ("9".equals(result)) {
            msg = "操作失败，请重试！";
        } else if ("10".equals(result)) {
            msg = "操作成功！";
        }
            return ajaxResult.put("res", msg);
    }

    @Override
    public AjaxResult jobTpAdd(String trayCode, int quantity) {
        String msg = "";
        AjaxResult ajaxResult = new AjaxResult();
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_auto_in_tp_one");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_tray_code", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_quantity", Integer.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);

        parameter.setParameter("v_tray_code", trayCode);
        parameter.setParameter("v_quantity", quantity);

        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");

        if ("1".equals(result)) {
            msg = "托盘已有入库作业！";
        } else if ("2".equals(result)) {
            msg = "托盘已有库存！";
        } else if ("3".equals(result)) {
            msg = "该口已有入库作业！";
        }  else if ("4".equals(result)) {
            msg = "扫描信息不存在！";
        }    else if ("9".equals(result)) {
            msg = "操作失败，请重试！";
        } else if ("10".equals(result)) {
            msg = "操作成功！";
        }
        return ajaxResult.put("res", msg);
    }
    @Override
    public AjaxResult jobTpOut(String exit_id, int quantity) {
        String msg = "";
        AjaxResult ajaxResult = new AjaxResult();
        for (int i = 0; i < quantity; i++) {

            StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_auto_out_tp");
            StoredProcedureQuery parameter = storedProcedureQuery
                    .registerStoredProcedureParameter("v_type", String.class, ParameterMode.IN)
                    .registerStoredProcedureParameter("v_exit_id", String.class, ParameterMode.IN)
                    .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);

            parameter.setParameter("v_type", "2");
            parameter.setParameter("v_exit_id", exit_id);

            //执行
            storedProcedureQuery.execute();
            //获取返回结果
            String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");

            if ("1".equals(result)) {
                msg = "库存中已经没有空托盘！";
            } else if ("9".equals(result)) {
                msg = "操作失败，请重试！";
            } else if ("10".equals(result)) {
                msg = "操作成功！";
            }
        }
        return ajaxResult.put("res", msg);
    }
}
